<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1OrderNodeExchangeLog;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * IP操作记录
 */
class V1OrderNodeExchangeLogController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1OrderNodeExchangeLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1OrderNodeExchangeLog;
    }

    /**
     * 浏览
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $order_node_id = $request->input('order_node_id');
        return view('v1-order-node-exchange-log/index', compact('order_node_id'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $query = $this->doSelect($where, $field, $order);

        // 查询ip
        $ips = $request->input('ips');
        if ($ips) {
            $name_array = getLineBreak($ips);
            if($name_array){
                $query->where(function ($query) use ($name_array) {
                    foreach ($name_array as $value) {
                        // 嵌套 where 实现 OR 条件
                        $query->orWhere('new_info', 'like', '%' . $value . '%');
                    }
                });
            }
        }
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-order-node-exchange-log/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-order-node-exchange-log/update');
    }

}
